package com.lhdx.practical.dao;

import com.lhdx.practical.bean.StudentGroup;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface StudentGroupDao extends CrudRepository<StudentGroup,Long> {
    StudentGroup findById(long id);
    StudentGroup findByFounderid(long founderid);
    void deleteById(long id);
    List<StudentGroup> findAllByDepartidAndStateOrderByCreatetimeDesc(long departid,int state);

    List<StudentGroup> findAllByState(int state);

    List<StudentGroup> findAllByStateAndGradeid(int state,long gradeid);

    Page<StudentGroup> findAll(Specification<StudentGroup> spec, Pageable pageable);

    /**
     * 修改小组人数
     * @param id
     * @param groupcount
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update studentgroup set groupcount = ?2 where id = ?1",nativeQuery = true)
    void updateGroupCount(long id,int groupcount);

    /**
     * 修改小组状态
     * @param id
     * @param state
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update studentgroup set state = ?2 where id = ?1",nativeQuery = true)
    void updateState(long id,int state);
}
